﻿using System;
using System.IO;

namespace SandCastleRunner
{
    /// <summary>
    /// Represents the SandCastle directory.
    /// </summary>
    class SandCastleDirectory
    {
        private string sandCastleDirectoryPath;

        /// <summary>
        /// Creates a new instance of SandCastleDirectory.
        /// </summary>
        /// <param name="sandCastleDirectoryPath">A string</param>
        public SandCastleDirectory(string sandCastleDirectoryPath)
        {
            SandCastleDirectoryPath = sandCastleDirectoryPath;
        }

        public string SandCastleDirectoryPath
        {
            get { return sandCastleDirectoryPath; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("SandCastleDirectoryPath");

                if (!Directory.Exists(value))
                    throw new DirectoryNotFoundException("SandCastle directory is missing.");

                sandCastleDirectoryPath = value;
            }
        }

        public string MrefBuilderPath
        {
            get
            {
                if (sandCastleDirectoryPath == null)
                    throw new InvalidOperationException("No SandCastle directory.");

                string fullMrefBuilderPath = sandCastleDirectoryPath + @"\ProductionTools\MRefBuilder.exe";
                if (!File.Exists(fullMrefBuilderPath))
                    throw new FileNotFoundException("MRefBuilder.exe not found.");

                return '"' + fullMrefBuilderPath + '"';
            }
        }

        public string XslTransformPath
        {
            get
            {
                if (sandCastleDirectoryPath == null)
                    throw new InvalidOperationException("No SandCastle directory.");

                string fullXslTransformPath = sandCastleDirectoryPath + @"\ProductionTools\XslTransform.exe";
                if (!File.Exists(fullXslTransformPath))
                    throw new FileNotFoundException("XslTransform.exe not found.");

                return '"' + fullXslTransformPath + '"';
            }
        }

        public string BuildAssemblerPath
        {
            get
            {
                if (sandCastleDirectoryPath == null)
                    throw new InvalidOperationException("No SandCastle directory.");

                string fullBuildAssemblerPath = sandCastleDirectoryPath + @"\ProductionTools\BuildAssembler.exe";
                if (!File.Exists(fullBuildAssemblerPath))
                    throw new FileNotFoundException("BuildAssembler.exe not found.");

                return '"' + fullBuildAssemblerPath + '"';
            }
        }

        public string ChmBuilderPath
        {
            get
            {
                if (sandCastleDirectoryPath == null)
                    throw new InvalidOperationException("No SandCastle directory.");

                string fullChmBuilderPath = sandCastleDirectoryPath + @"\ProductionTools\ChmBuilder.exe";
                if (!File.Exists(fullChmBuilderPath))
                    throw new FileNotFoundException("ChmBuilder.exe not found.");

                return '"' + fullChmBuilderPath + '"';
            }
        }

        public string GetXslTransformationPath(string xslTransformationFile)
        {
            if (sandCastleDirectoryPath == null)
                throw new InvalidOperationException("No SandCastle directory.");

            string xslTransformationPath = sandCastleDirectoryPath + @"\ProductionTransforms\" + xslTransformationFile;
            if (!File.Exists(xslTransformationPath))
                throw new FileNotFoundException("Xsl Transformation File not found.");

            return '"' + xslTransformationPath + '"';
        }

        public string GetDirectoryBuilderPath(string presentation)
        {
            if (sandCastleDirectoryPath == null)
                throw new InvalidOperationException("No SandCastle directory.");

            string presentationDirectoryPath = sandCastleDirectoryPath + @"\Presentation\" + presentation;
            if (!Directory.Exists(presentationDirectoryPath))
                throw new DirectoryNotFoundException("Presentation directory " + presentation + " not found.");

            string directoryBuilderPath = presentationDirectoryPath + @"\copyOutput.bat";
            if (!File.Exists(directoryBuilderPath))
                throw new FileNotFoundException(directoryBuilderPath + " not found.");

            return '"' + directoryBuilderPath + '"';
        }

        public string GetConfigFilePath(string presentation)
        {
            if (sandCastleDirectoryPath == null)
                throw new InvalidOperationException("No SandCastle directory.");

            string presentationDirectoryPath = sandCastleDirectoryPath + @"\Presentation\" + presentation;
            if (!Directory.Exists(presentationDirectoryPath))
                throw new DirectoryNotFoundException("Presentation directory " + presentation + " not found.");

            string configFilePath = presentationDirectoryPath + @"\configuration\sandcastle.config";
            if (!File.Exists(configFilePath))
                throw new FileNotFoundException(configFilePath + " not found.");

            return '"' + configFilePath + '"';
        }
    }
}
